<?php

namespace control;
/*
*委外用料差异类
*/

class outsourceDiffClass extends adIndexClass
{
    private const page = 'outsourceDiff';
    private const title = '委外用料差异';
    private const table = 'outsource_process';
    private const tableMx = 'outsource_process_detail';
    private const table_material = 'outsource_process_material';

    /**
     * 字段初始化
     */
    public function fieldInit($fun): array
    {
        switch ($fun) {
            case 'statisticsSearch':
                $tr = [
                    'supplier'    => [
                        'name'  => '委外单位',
                        'width' => 150,
                    ],
                    'code'        => [
                        'name'  => "物料编码",
                        'width' => 200,
                    ],
                    'name'        => [
                        'name'  => "物料名称",
                        'width' => 200,
                    ],
                    'model'       => [
                        'name'  => "规格型号",
                        'width' => 150,
                    ],
                    'unit'        => [
                        'name'  => "单位",
                        'width' => 80,
                    ],
                    'process'     => [
                        'name'  => '工序',
                        'width' => 100,
                    ],
                    'expectedNum' => [
                        'name'  => '预计用量',
                        'width' => 100,
                    ],
                    'pickNum'     => [
                        'name'  => '发料数量',
                        'width' => 100,
                    ],
                    'refundNum'   => [
                        'name'  => '退料数量',
                        'width' => 100,
                    ],
                    'reality'     => [
                        'name'  => '实际发料',
                        'width' => 100,
                    ],
                    'theorynum'   => [
                        'name'  => '已收货理论用料',
                        'width' => 120,
                    ],
                    'decNum'      => [
                        'name'  => '已冲减数量',
                        'width' => 100,
                    ],
                    'stock'       => [
                        'name'  => '理论库存',
                        'width' => 100,
                    ]
                ];
                break;
            default:
                $tr = [
                    'recordDate'   => [
                        'name'  => '单据日期',
                        'width' => 100,
                        'sort'  => 1,
                    ],
                    'orderNo'      => [
                        'name'  => '单据编号',
                        'width' => 200,
                        'sort'  => 1,
                    ],
                    'supplier'     => [
                        'name'  => '委外单位',
                        'width' => 100,
                    ],
                    'code'         => [
                        'name'  => "物料编码",
                        'width' => 100,
                        'sort'  => 1,
                    ],
                    'materialName' => [
                        'name'  => "物料名称",
                        'width' => 100,
                    ],
                    'model'        => [
                        'name'  => "规格型号",
                        'width' => 100,
                    ],
                    'unit'         => [
                        'name'  => "单位",
                        'width' => 100,
                    ],
                    'process'      => [
                        'name'  => '工序',
                        'width' => 100,
                    ],
                    'quantity'     => [
                        'name'  => '预计用量',
                        'width' => 100,
                    ],
                    'pickNum'      => [
                        'name'  => '发料数量',
                        'width' => 100,
                    ],
                    'refundNum'    => [
                        'name'  => '退料数量',
                        'width' => 100,
                    ],
                    'reality'      => [
                        'name'  => '实际发料',
                        'width' => 100,
                    ],
                    'num'          => [
                        'name'  => '已收货理论用料',
                        'width' => 120,
                    ],
                    'decNum'       => [
                        'name'  => '已冲减数量',
                        'width' => 100,
                    ],
                    'stock'        => [
                        'name'  => '理论库存',
                        'width' => 100,
                    ]
                ];
        }
        return $tr;
    }

    /*列表页-查询条件*/
    public function homeOptions($isAsync = true)
    {
        //生成查询表单
        $html = $this->tableShow([
            [
                [
                    '委外单位' => selectMultiple(["name" => "supplierId", 'table' => "supplier", 'sqlId' => 'id', 'where' => "coid = '" . self::$coid . "' and outsource = '是'", 'sqlField' => 'name', 'class' => 'width200']),
                    "单据编号" => text(array("name" => "orderNo")),
                    "物料编码" => text(array("name" => "code")),
                ],
                [
                    "物料名称" => text(array("name" => "materialName")),
                    "工序"   => text(array("name" => "process")),
                    "单据日期" => text(array("name" => "createTime", 'laydate' => ['type' => 'date', 'range' => '~'])),
                ]
            ]
        ], 1);
        //返回
        if (!$isAsync) {
            return $html;
        } else {
            return json_encode(['html' => $html], 320);
        }
    }

    /*列表-查询条件*/
    public function home()
    {
        $html = $this->homeOptions(false);
        //控件
        $button = "";
        if ($this->power(self::page, "export")) {
            $button .= "<span class='spanButton' onclick=\"layer('importTemplate','exportFrame',{cla:'" . self::page . "'})\">导出</span>";
        }
        return $this->homeFun($this->search($html, layout: 'pop', toolButton: $button));
    }


    /*客户列表-查询结果*/
    public function homeSearch()
    {
        //like查询数组
        $like = array(
            "b.orderNo" => $this->post('orderNo'),
            "c.name"    => $this->post('materialName'),
            "c.code"    => $this->post('code'),
        );
        //全等查询数组
        $equal = array(
            "b.coid"  => self::$coid,
            "b.state" => '已确认'
        );
        $and   = '';
        if ($this->post('createTime')) {
            //单据日期
            $and .= self::timeComparison($this->post('createTime'), field: 'b.recordDate');
        }
        if ($this->post('process')) {
            $and .= " and a.processId in (select id from process where name like '%" . $this->post('process') . "%') ";
        }
        //排序
        $orderBy = $this->listSort(array(
            "search" => $this->post('list_sort_key') ?: 'b.recordDate desc,a.list',
            "sort"   => $this->post('list_sort') ?: "desc",
            "array"  => array(),
        ));
        //获取查询结果
        $where                       = search(array("like" => $like, "=" => $equal)) . $and;
        $page                        = $this->page(array(
            "table" => self::tableMx . " as a join " . self::table . " as b on a.orderId = b.id  join material as c on c.id = a.materialId",
            'field' => "a.*,b.id as outsourceId,b.orderNo,b.recordDate,b.supplierId,c.code,c.name as materialName,c.model",
            "where" => $where,
            "order" => $orderBy['orderBy']
        ));
        $_SESSION[self::page]['sql'] = $page['fullSql'];
        //获取自定义字段参数
        $installSearch = $this->installSearch();
        $body          = [];
        //表格控件区
        $liButton = array(
            $this->checkAll("listForm", "listId")
        );
        //获取记录
        foreach ($page['array'] as $array) {
            //列表控件
            $dominate   = array(
                "box" => array("name" => "listId", "value" => $array['id']),
            );
            $liButton[] = $this->dominate($dominate);
            $material   = query('material', "id = '$array[materialId]'");
            //发料
            $pickNum      = 0;
            $pickOrderAll = find(array(
                'table' => 'outsource_pick_order as a join outsource_pick_order_detail as b on a.id = b.orderId',
                "where" => "a.state = '已确认' and a.type=0 and b.sourceId = '$array[id]' and b.materialId = '$array[materialId]'"
            ));
            foreach ($pickOrderAll['array'] as $val) {
                $inventoryAll = find(array('table' => 'inventory_detail', 'where' => "sourceId = '$val[id]'"));
                foreach ($inventoryAll['array'] as $inventory) {
                    $pickNum += machiningOrderClass::material_num_change($val['materialId'], $inventory['unit'], $material['unit'], $inventory['num']);
                }
            }
            $pickNum = self::formattingFigure($pickNum, 'numPrecision');
            //退料
            $refundNum      = 0;
            $refundOrderAll = find(array(
                'table' => 'outsource_pick_order as a join outsource_pick_order_detail as b on a.id = b.orderId',
                "where" => "a.state = '已确认' and a.type=1 and b.sourceId = '$array[id]' and b.materialId = '$array[materialId]'"
            ));
            foreach ($refundOrderAll['array'] as $val) {
                $inventoryRefundAll = find(array('table' => 'inventory_detail', 'where' => "sourceId = '$val[id]'"));
                foreach ($inventoryRefundAll['array'] as $refundInventory) {
                    $refundNum += machiningOrderClass::material_num_change($val['materialId'], $refundInventory['unit'], $material['unit'], $refundInventory['num']);
                }
            }
            $refundOrderAll = find(array(
                'table' => 'outsource_pick_order as a join outsource_pick_order_detail as b on a.id = b.orderId',
                "where" => "a.state = '已确认' and a.type=1 and b.sourceId in (select id from outsource_pick_order_detail where sourceId = '$array[id]') and b.materialId = '$array[materialId]'"
            ));
            foreach ($refundOrderAll['array'] as $val) {
                $inventoryRefundAll = find(array('table' => 'inventory_detail', 'where' => "sourceId = '$val[id]'"));
                foreach ($inventoryRefundAll['array'] as $refundInventory) {
                    $refundNum += machiningOrderClass::material_num_change($val['materialId'], $refundInventory['unit'], $material['unit'], $refundInventory['num']);
                }
            }
            $refundNum = self::formattingFigure($refundNum, 'numPrecision');

            $num    = 0;
            $resAll = find(array('table' => self::table_material, 'where' => "orderId = '$array[outsourceId]' and id = '$array[targetSkuId]'"));
            foreach ($resAll['array'] as $rArray) {
                //已收货数量
                $receiveNum  = 0;
                $getOrderAll = find(array(
                    'table' => 'outsource_in_order as a join outsource_in_order_detail as b on a.id = b.orderId',
                    "where" => "a.state = '已确认' and b.sourceId = '$rArray[id]' and b.materialId = '$rArray[materialId]'"
                ));
                foreach ($getOrderAll['array'] as $val) {
                    $inventoryRefundAll = find(array('table' => 'inventory_detail', 'where' => "sourceId = '$val[id]'"));
                    foreach ($inventoryRefundAll['array'] as $refundInventory) {
                        $receiveNum += machiningOrderClass::material_num_change($val['materialId'], $refundInventory['unit'], $rArray['unit'], $refundInventory['num']);
                    }
                    $refundMaterialNum = 0;
                    //退货数量
                    $all = find(array(
                        'table' => 'outsource_refund_order as a join outsource_refund_order_detail as b on a.id = b.orderId',
                        'field' => "b.*", "where" => "a.state = '已确认' and b.sourceId = '$val[id]' and b.materialId = '$val[materialId]'"
                    ));
                    foreach ($all['array'] as $item) {
                        $inventoryRefundAll = find(array('table' => 'inventory_detail', 'where' => "sourceId = '$val[id]'"));
                        foreach ($inventoryRefundAll['array'] as $refundInventory) {
                            $refundMaterialNum += machiningOrderClass::material_num_change($item['materialId'], $refundInventory['unit'], $rArray['unit'], $refundInventory['num']);
                        }
                    }
                    $receiveNum = $receiveNum - $refundMaterialNum;//收货减退货的
                }
                $num += machiningOrderClass::material_num_change(
                    $array['materialId'],
                    $material['unit'],
                    $array['unit'],
                    $this->bomNum($rArray['bomId'], $rArray['materialId'], $receiveNum, $rArray['unit'], $array['materialId'])
                );
            }
            //已冲减数量
            $decNum = find(array(
                'table' => 'outsource_settle_material',
                'where' => "outsourceMaterialId = '$array[targetSkuId]' and materialId = '$array[materialId]'",
                'sum'   => "num"
            ));
            //本行初始化
            $row = [];
            foreach ($installSearch['key'] as $key) {
                switch ($key) {
                    case "supplier":
                        $row[] = supplierClass::getName($array['supplierId']);
                        break;
                    case "process":
                        $process = query('process', "id = '$array[processId]'", "name");
                        $row[]   = $process['name'];
                        break;
                    case "quantity":
                        $row[] = self::lostZero($array['quantity'], 'numPrecision');
                        break;
                    case "pickNum":
                        $row[] = $pickNum;
                        break;
                    case "refundNum":
                        $row[] = $refundNum;
                        break;
                    case "reality":
                        $row[] = self::formattingFigure($pickNum - $refundNum, 'numPrecision');
                        break;
                    case "num":
                        $row[] = $num;
                        break;
                    case "decNum":
                        $row[] = self::lostZero($decNum, 'numPrecision');
                        break;
                    case "stock":
                        $row[] = self::formattingFigure($pickNum - $refundNum - $decNum - $num, 'numPrecision');
                        break;
                    default:
                        $row[] = isset($array[$key]) && $array[$key] ? $array[$key] : '';
                }
            }
            $body[] = $row;
        }
        //预计用量
        $totalQuery = find([
            'table'  => self::tableMx . " as a join " . self::table . " as b on a.orderId = b.id  join material as c on c.id = a.materialId",
            'single' => $where,
            'field'  => "ifnull(sum(a.quantity),0) as quantity",
        ]);
        //发料
        $pickNum      = 0;
        $pickOrderAll = find(array(
            'table' => 'outsource_pick_order as ord join outsource_pick_order_detail as detail on ord.id = detail.orderId',
            "where" => "ord.state = '已确认' and ord.type=0 and detail.sourceId in (select a.id from " . self::tableMx . " as a join " . self::table . " as b on a.orderId = b.id  join material as c on c.id = a.materialId where 1=1 {$where})"
        ));
        foreach ($pickOrderAll['array'] as $val) {
            $inventoryAll = find(array('table' => 'inventory_detail', 'where' => "sourceId = '$val[id]'"));
            foreach ($inventoryAll['array'] as $inventory) {
                // 转换主单位
                $pickNum += inventoryBalanceClass::toMainUnit($val['materialId'], $inventory['num'], $inventory['unit']);
            }
        }
        $pickNum = self::formattingFigure($pickNum, 'numPrecision');
        //退料
        $refundNum      = 0;
        $refundOrderAll = find(array(
            'table' => 'outsource_pick_order as ord join outsource_pick_order_detail as detail on ord.id = detail.orderId',
            "where" => "ord.state = '已确认' and ord.type=1 and detail.sourceId in (select a.id from " . self::tableMx . " as a join " . self::table . " as b on a.orderId = b.id  join material as c on c.id = a.materialId where 1=1 {$where})"
        ));
        foreach ($refundOrderAll['array'] as $val) {
            $inventoryRefundAll = find(array('table' => 'inventory_detail', 'where' => "sourceId = '$val[id]'"));
            foreach ($inventoryRefundAll['array'] as $refundInventory) {
                // 转换主单位
                $refundNum += inventoryBalanceClass::toMainUnit($val['materialId'], $refundInventory['num'], $refundInventory['unit']);
            }
        }
        $refundOrderAll = find(array(
            'table' => 'outsource_pick_order as ord join outsource_pick_order_detail as detail on ord.id = detail.orderId',
            "where" => "ord.state = '已确认' and ord.type=1 and detail.sourceId in (select id from outsource_pick_order_detail where sourceId in (select a.id from " . self::tableMx . " as a join " . self::table . " as b on a.orderId = b.id  join material as c on c.id = a.materialId where 1=1 {$where}))"
        ));
        foreach ($refundOrderAll['array'] as $val) {
            $inventoryRefundAll = find(array('table' => 'inventory_detail', 'where' => "sourceId = '$val[id]'"));
            foreach ($inventoryRefundAll['array'] as $refundInventory) {
                // 转换主单位
                $refundNum += inventoryBalanceClass::toMainUnit($val['materialId'], $refundInventory['num'], $refundInventory['unit']);
            }
        }
        $refundNum = self::formattingFigure($refundNum, 'numPrecision');

        $num    = 0;
        $resAll = find(array(
            'table' => self::table_material . " as mat join " . self::tableMx . " as a on a.targetSkuId = mat.id join " . self::table . " as b on a.orderId = b.id  join material as c on c.id = a.materialId ",
            'field' => "mat.*,a.materialId as productId,c.unit as toUnit",
            'where' => $where
        ));
        foreach ($resAll['array'] as $rArray) {
            //已收货数量
            $receiveNum  = 0;
            $getOrderAll = find(array(
                'table' => 'outsource_in_order as a join outsource_in_order_detail as b on a.id = b.orderId',
                "where" => "a.state = '已确认' and b.sourceId = '$rArray[id]' and b.materialId = '$rArray[materialId]'"
            ));
            foreach ($getOrderAll['array'] as $val) {
                $inventoryRefundAll = find(array('table' => 'inventory_detail', 'where' => "sourceId = '$val[id]'"));
                foreach ($inventoryRefundAll['array'] as $refundInventory) {
                    $receiveNum += machiningOrderClass::material_num_change($val['materialId'], $refundInventory['unit'], $rArray['unit'], $refundInventory['num']);
                }
                $refundMaterialNum = 0;
                //退货数量
                $all = find(array(
                    'table' => 'outsource_refund_order as a join outsource_refund_order_detail as b on a.id = b.orderId',
                    'field' => "b.*", "where" => "a.state = '已确认' and b.sourceId = '$val[id]' and b.materialId = '$val[materialId]'"
                ));
                foreach ($all['array'] as $item) {
                    $inventoryRefundAll = find(array('table' => 'inventory_detail', 'where' => "sourceId = '$val[id]'"));
                    foreach ($inventoryRefundAll['array'] as $refundInventory) {
                        $refundMaterialNum += machiningOrderClass::material_num_change($item['materialId'], $refundInventory['unit'], $rArray['unit'], $refundInventory['num']);
                    }
                }
                $receiveNum = $receiveNum - $refundMaterialNum;//收货减退货的
            }
            $num += machiningOrderClass::material_num_change(
                $rArray['productId'],
                $rArray['unit'],
                $rArray['toUnit'],
                $this->bomNum($rArray['bomId'], $rArray['materialId'], $receiveNum, $rArray['unit'], $rArray['productId'])
            );
        }
        //已冲减数量
        $decNum = find(array(
            'table' => 'outsource_settle_material',
            'where' => "outsourceMaterialId in (select a.targetSkuId from " . self::tableMx . " as a join " . self::table . " as b on a.orderId = b.id  join material as c on c.id = a.materialId where 1=1 {$where})",
            'sum'   => "num"
        ));
        $total  = [
            'quantity'   => self::lostZero($totalQuery['quantity'], 'numPrecision'),
            'pickNum'   => $pickNum,
            'refundNum' => $refundNum,
            'reality'   => self::formattingFigure($pickNum - $refundNum, 'numPrecision'),
            'num'       => $num,
            'decNum'    => self::lostZero($decNum, 'numPrecision'),
            'stock'     => self::formattingFigure($pickNum - $refundNum - $decNum - $num, 'numPrecision'),
        ];
        //生成页面
        $li = $this->li(array(
            "body"        => $body,
            "install"     => $installSearch,
            "button"      => array("html" => $liButton),
            "orderBySign" => $orderBy,
            "page"        => $page['para'],
            "total"       => $total,
        ));
        //返回
        $json['html'] = "<form name='listForm'>" . $li . "</form>" . $page['button'] . clear;
        return json_encode($json, 320);
    }


    /**
     * 理论用料数量
     * @param $bomId
     * @param $productId
     * @param $num
     * @param $unit
     * @param $materialId
     * @return float|int
     */
    private function bomNum($bomId, $productId, $num, $unit, $materialId): float|int
    {
        $material_list = machiningOrderClass::bomMx($bomId, $productId, $num, $unit);
        $useNum        = 0;
        foreach ($material_list as $array) {
            if ($array['materialId'] == $materialId) {
                $useNum += $array['quantity'];
            }
        }
        return self::formattingFigure($useNum, 'numPrecision');
    }

    /**
     * 导出
     */
    public function exportData()
    {
        $templateId     = $this->get('templateId');
        $checkedId      = $this->get('checkedId') ? explode(',', $this->get('checkedId')) : [];
        if (!$this->power(self::page, 'export')) {
            $_SESSION['warn'] = '你还没有导出的权限';
        } else {
            $data = [];
            /***********标题**********/
            $title = [];
            $tempFieldAll = importTemplateClass::templateField(self::page, 'exportFieldInit', id:$templateId);
            foreach ($tempFieldAll as $array) {
                if ($array['display']) {
                    $title[] = $array['aliasName'];
                }
            }
            //判断是否选中导出
            if (!self::is_null_array($checkedId)) {
                $result = find(array(
                    "table" => self::table,
                    "where" => "id in ('" . implode("','", $checkedId) . "') order by time desc",
                ))['array'];
            } else {
                $sql    = $_SESSION[self::page]['sql'];
                $result = sqlFind($sql);
            }
            //导出数据生成
            foreach ($result as $array) {
                $column   = [];
                $material = query('material', "id = '$array[materialId]'");
                $process  = query('process', "id = '$array[processId]'", "name");
                //发料
                $pickNum      = 0;
                $pickOrderAll = find(array(
                    'table' => 'outsource_pick_order as a join outsource_pick_order_detail as b on a.id = b.orderId',
                    "where" => "a.state = '已确认' and a.type=0 and b.sourceId = '$array[id]' and b.materialId = '$array[materialId]'"
                ));
                foreach ($pickOrderAll['array'] as $val) {
                    $inventoryAll = find(array('table' => 'inventory_detail', 'where' => "sourceId = '$val[id]'"));
                    foreach ($inventoryAll['array'] as $inventory) {
                        $pickNum += machiningOrderClass::material_num_change($val['materialId'], $inventory['unit'], $material['unit'], $inventory['num']);
                    }
                }
                $pickNum = self::formattingFigure($pickNum, 'numPrecision');
                //退料
                $refundNum      = 0;
                $refundOrderAll = find(array(
                    'table' => 'outsource_pick_order as a join outsource_pick_order_detail as b on a.id = b.orderId',
                    "where" => "a.state = '已确认' and a.type=1 and b.sourceId = '$array[id]' and b.materialId = '$array[materialId]'"
                ));
                foreach ($refundOrderAll['array'] as $val) {
                    $inventoryRefundAll = find(array('table' => 'inventory_detail', 'where' => "sourceId = '$val[id]'"));
                    foreach ($inventoryRefundAll['array'] as $refundInventory) {
                        $refundNum += machiningOrderClass::material_num_change($val['materialId'], $refundInventory['unit'], $material['unit'], $refundInventory['num']);
                    }
                }
                $refundOrderAll = find(array(
                    'table' => 'outsource_pick_order as a join outsource_pick_order_detail as b on a.id = b.orderId',
                    "where" => "a.state = '已确认' and a.type=1 and b.sourceId in (select id from outsource_pick_order_detail where sourceId = '$array[id]') and b.materialId = '$array[materialId]'"
                ));
                foreach ($refundOrderAll['array'] as $val) {
                    $inventoryRefundAll = find(array('table' => 'inventory_detail', 'where' => "sourceId = '$val[id]'"));
                    foreach ($inventoryRefundAll['array'] as $refundInventory) {
                        $refundNum += machiningOrderClass::material_num_change($val['materialId'], $refundInventory['unit'], $material['unit'], $refundInventory['num']);
                    }
                }
                $refundNum = self::formattingFigure($refundNum, 'numPrecision');
                $supplier  = query('supplier', "id = '$array[supplierId]'");

                $num    = 0;
                $resAll = find(array('table' => self::table_material, 'where' => "orderId = '$array[outsourceId]' and id = '$array[targetSkuId]'"));
                foreach ($resAll['array'] as $rArray) {
                    //已收货数量
                    $receiveNum  = 0;
                    $getOrderAll = find(array(
                        'table' => 'outsource_in_order as a join outsource_in_order_detail as b on a.id = b.orderId',
                        "where" => "a.state = '已确认' and b.sourceId = '$rArray[id]' and b.materialId = '$rArray[materialId]'"
                    ));
                    foreach ($getOrderAll['array'] as $val) {
                        $inventoryRefundAll = find(array('table' => 'inventory_detail', 'where' => "sourceId = '$val[id]'"));
                        foreach ($inventoryRefundAll['array'] as $refundInventory) {
                            $receiveNum += machiningOrderClass::material_num_change($val['materialId'], $refundInventory['unit'], $rArray['unit'], $refundInventory['num']);
                        }
                        $refundMaterialNum = 0;
                        //退货数量
                        $refundall = find(array(
                            'table' => 'outsource_refund_order as a join outsource_refund_order_detail as b on a.id = b.orderId',
                            'field' => "b.*",
                            "where" => "a.state = '已确认' and b.sourceId = '$val[id]' and b.materialId = '$val[materialId]'"
                        ));
                        foreach ($refundall['array'] as $item) {
                            $inventoryRefundAll = find(array('table' => 'inventory_detail', 'where' => "sourceId = '$val[id]'"));
                            foreach ($inventoryRefundAll['array'] as $refundInventory) {
                                $refundMaterialNum += machiningOrderClass::material_num_change($item['materialId'], $refundInventory['unit'], $rArray['unit'], $refundInventory['num']);
                            }
                        }
                        $receiveNum = $receiveNum - $refundMaterialNum;//收货减退货的
                    }
                    $num += machiningOrderClass::material_num_change(
                        $array['materialId'],
                        $material['unit'],
                        $array['unit'],
                        $this->bomNum($rArray['bomId'], $rArray['materialId'], $receiveNum, $rArray['unit'], $array['materialId'])
                    );
                }
                //已冲减数量
                $decNum = find(array('table' => 'outsource_settle_material', 'where' => "outsourceMaterialId = '$array[targetSkuId]' and materialId = '$array[materialId]'", 'sum' => "num"));

                foreach ($tempFieldAll as $templateField) {
                    if ($templateField['display']) {
                        $column[] = match ($templateField['columnName']) {
                            '单据日期' => $array['recordDate'],
                            '单据编号' => $array['orderNo'],
                            '委外单位' => $supplier['name'],
                            '物料编码' => $array['code'],
                            '物料名称' => $array['materialName'],
                            '规格型号' => $array['model'],
                            '单位' => $array['unit'],
                            '工序' => $process['name'],
                            '预计用量' => self::lostZero($array['quantity'], 'numPrecision'),
                            '发料数量' => $pickNum,
                            '退料数量' => $refundNum,
                            '实际发料' => self::formattingFigure($pickNum - $refundNum, 'numPrecision'),
                            '已收货理论用料' => $num,
                            '已冲减数量' => self::lostZero($decNum, 'numPrecision'),
                            '理论库存' => self::formattingFigure($pickNum - $refundNum - $decNum - $num, 'numPrecision'),
                            default => '',
                        };
                    }
                }
                $data[] = $column;
            }
            $text = $this->control['name'] . "导出了委外用料差异明细";
            $this->logText(["type" => self::page, "text" => $text]);
            $this->excelOut($title, $data, "委外用料差异明细.xlsx");
            exit(0);
        }
        header("Location:" . getenv("HTTP_REFERER"));
    }

    /*列表页-查询条件*/
    public function statisticsOptions($isAsync = true)
    {
        //生成查询表单
        $html = $this->tableShow([
            [
                [
                    '委外单位' => selectMultiple(["name" => "supplierId", 'table' => "supplier", 'sqlId' => 'id', 'where' => "coid = '" . self::$coid . "' and outsource = '是'", 'sqlField' => 'name', 'class' => 'width200']),
                    "物料编码" => text(array("name" => "code")),
                    "物料名称" => text(array("name" => "materialName")),
                ],
                [
                    "工序"   => text(array("name" => "process")),
                    "单据日期" => text(array("name" => "createTime", 'laydate' => ['type' => 'date', 'range' => '~'])),
                ]
            ]
        ], 1);
        //返回
        if (!$isAsync) {
            return $html;
        } else {
            return json_encode(['html' => $html], 320);
        }
    }

    public function statistics()
    {
        $html = $this->statisticsOptions(false);
        //控件
        $button = "";
        if ($this->power('outsourceDiffStatistics', "export")) {
            $button .= "<span class='spanButton' onclick=\"layer('importTemplate','exportFrame',{cla:'" . self::page . "',exportFun:'exportDataOther',classify:'委外用料差异汇总'})\">导出</span>";
        }
        return $this->homeFun($this->search($html, layout: 'pop', toolButton: $button));
    }

    /**
     * 汇总
     * @return bool|string
     */
    public function statisticsSearch()
    {
        //like查询数组
        $like = array(
            "b.orderNo" => $this->post('orderNo'),
            "c.name"    => $this->post('materialName'),
            "c.code"    => $this->post('code'),
        );
        //全等查询数组
        $equal = array(
            "b.coid"  => self::$coid,
            "b.state" => '已确认'
        );
        $and   = '';
        if ($this->post('createTime')) {
            //单据日期
            $and .= self::timeComparison($this->post('createTime'), field: 'b.recordDate');
        }
        if ($this->post('process')) {
            $and .= " and a.processId in (select id from process where name like '%" . $this->post('process') . "%') ";
        }
        //获取查询结果
        $where = search(array("like" => $like, "=" => $equal)) . $and;

        //like查询数组
        $likeTwo = array(
            "b.orderNo" => $this->post('orderNo'),
        );
        //全等查询数组
        $equalTwo = array(
            "b.coid"  => self::$coid,
            "b.state" => '已确认'
        );
        //获取查询结果
        $whereTwo = search(array("like" => $likeTwo, "=" => $equalTwo));

        if ($this->post('createTime')) {
            //单据日期
            $whereTwo .= self::timeComparison($this->post('createTime'), field: 'b.recordDate');
        }
        if ($this->post('process')) {
            $whereTwo .= " and a.processId in (select id from process where name like '%" . $this->post('process') . "%') ";
        }

        //排序
        $orderBy                          = $this->listSort(array(
            "search" => $this->post('list_sort_key') ?: 'supplierId',
            "sort"   => $this->post('list_sort') ?: "desc",
            "array"  => array(),
        ));
        $page                             = $this->page(array(
            "table" => self::tableMx . " as a join " . self::table . " as b on a.orderId = b.id  join material as c on c.id = a.materialId",
            'field' => "distinct b.supplierId,a.materialId,a.processId",
            "where" => $where,
            "order" => $orderBy['orderBy']
        ));
        $_SESSION[self::page]['stasql']   = $page['fullSql'];
        $_SESSION[self::page]['where']    = $where;
        $_SESSION[self::page]['whereTwo'] = $whereTwo;
        //获取自定义字段参数
        $installSearch = $this->installSearch();
        $body          = [];
        //表格控件区
        $liButton = array();
        //获取记录
        foreach ($page['array'] as $array) {
            $material    = query('material', "id = '$array[materialId]'");
            $expectedNum = 0;//预计用量
            $pickNum     = 0;
            //退料
            $refundNum = 0;

            $theorynum = 0;//理论用料数量
            $resAll    = find(array(
                "table" => self::tableMx . " as a join " . self::table . " as b on a.orderId = b.id join material as c on c.id = a.materialId",
                'field' => "a.*,b.orderNo,b.recordDate,b.supplierId,c.code,c.name as materialName,c.model",
                "where" => $where . " and b.supplierId = '$array[supplierId]' and a.materialId = '$array[materialId]' and a.processId = '$array[processId]' order by b.recordDate desc,a.list asc"
            ));
            foreach ($resAll['array'] as $rArray) {
                $expectedNum += $rArray['num'];
                //发料
                $pickOrderAll = find(array(
                    'table' => 'outsource_pick_order as a join outsource_pick_order_detail as b on a.id = b.orderId',
                    "where" => "a.state = '已确认' and a.type=0 and b.sourceId = '$rArray[id]' and b.materialId = '$rArray[materialId]'"
                ));
                foreach ($pickOrderAll['array'] as $val) {
                    $inventoryAll = find(array('table' => 'inventory_detail', 'where' => "sourceId = '$val[id]'"));
                    foreach ($inventoryAll['array'] as $inventory) {
                        $pickNum += machiningOrderClass::material_num_change($val['materialId'], $inventory['unit'], $material['unit'], $inventory['num']);
                    }
                }
                $pickNum = self::formattingFigure($pickNum, 'numPrecision');

                $refundOrderAll = find(array(
                    'table' => 'outsource_pick_order as a join outsource_pick_order_detail as b on a.id = b.orderId',
                    "where" => "a.state = '已确认' and a.type=1 and b.sourceId = '$rArray[id]' and b.materialId = '$rArray[materialId]'"
                ));
                foreach ($refundOrderAll['array'] as $val) {
                    $inventoryRefundAll = find(array('table' => 'inventory_detail', 'where' => "sourceId = '$val[id]'"));
                    foreach ($inventoryRefundAll['array'] as $refundInventory) {
                        $refundNum += machiningOrderClass::material_num_change($val['materialId'], $refundInventory['unit'], $material['unit'], $refundInventory['num']);
                    }
                }
                $refundOrderAll = find(array(
                    'table' => 'outsource_pick_order as a join outsource_pick_order_detail as b on a.id = b.orderId',
                    "where" => "a.state = '已确认' and a.type=1 and b.sourceId in (select id from outsource_pick_order_detail where sourceId = '$rArray[id]') and b.materialId = '$rArray[materialId]'"
                ));
                foreach ($refundOrderAll['array'] as $val) {
                    $inventoryRefundAll = find(array('table' => 'inventory_detail', 'where' => "sourceId = '$val[id]'"));
                    foreach ($inventoryRefundAll['array'] as $refundInventory) {
                        $refundNum += machiningOrderClass::material_num_change($val['materialId'], $refundInventory['unit'], $material['unit'], $refundInventory['num']);
                    }
                }
            }
            $resAll = find(array(
                'table' => self::table_material . " as a join " . self::table . " as b on a.orderId = b.id",
                'field' => "a.*,b.id as outsourceId",
                'where' => $whereTwo . " and b.supplierId = '$array[supplierId]' and a.processId = '$array[processId]'"
            ));
            foreach ($resAll['array'] as $rArray) {
                //已收货数量-退货数量
                $receiveNum  = 0;
                $getOrderAll = find(array(
                    'table' => 'outsource_in_order as a join outsource_in_order_detail as b on a.id = b.orderId',
                    "where" => "a.state = '已确认' and b.sourceId = '$rArray[id]' and b.materialId = '$rArray[materialId]'"
                ));
                foreach ($getOrderAll['array'] as $val) {
                    $inventoryRefundAll = find(array('table' => 'inventory_detail', 'where' => "sourceId = '$val[id]'"));
                    foreach ($inventoryRefundAll['array'] as $refundInventory) {
                        $receiveNum += machiningOrderClass::material_num_change($val['materialId'], $refundInventory['unit'], $rArray['unit'], $refundInventory['num']);
                    }
                    $refundMaterialNum = 0;
                    //退货数量
                    $all = find(array(
                        'table' => 'outsource_refund_order as a join outsource_refund_order_detail as b on a.id = b.orderId',
                        "where" => "a.state = '已确认' and b.sourceId = '$val[id]' and b.materialId = '$val[materialId]'"
                    ));
                    foreach ($all['array'] as $item) {
                        $inventoryRefundAll = find(array('table' => 'inventory_detail', 'where' => "sourceId = '$val[id]'"));
                        foreach ($inventoryRefundAll['array'] as $refundInventory) {
                            $refundMaterialNum += machiningOrderClass::material_num_change($item['materialId'], $refundInventory['unit'], $rArray['unit'], $refundInventory['num']);
                        }
                    }
                    $receiveNum = $receiveNum - $refundMaterialNum;//收货减退货的
                }
                $theorynum += machiningOrderClass::material_num_change(
                    $rArray['materialId'],
                    $material['unit'],
                    $material['unit'],
                    $this->bomNum($rArray['bomId'], $rArray['materialId'], $receiveNum, $rArray['unit'], $array['materialId'])
                );
            }
            //已冲减数量
            $decNum = find(array(
                'table' => 'outsource_settle_material as a join outsource_settle as b on a.orderId = b.id',
                'where' => "b.supplierId = '$array[supplierId]' and materialId = '$array[materialId]'",
                'sum'   => "num"
            ));
            //本行初始化
            $row = [];
            foreach ($installSearch['key'] as $key) {
                switch ($key) {
                    case "supplier":
                        $row[] = supplierClass::getName($array['supplierId']);
                        break;
                    case "process":
                        $process = query('process', "id = '$array[processId]'", "name");
                        $row[]   = $process['name'];
                        break;
                    case "code":
                    case "name":
                    case "model":
                    case "unit":
                        $row[] = $material[$key];
                        break;
                    case "expectedNum":
                        $row[] = $expectedNum;
                        break;
                    case "pickNum":
                        $row[] = self::lostZero($pickNum, 'numPrecision');
                        break;
                    case "refundNum":
                        $row[] = self::lostZero($refundNum, 'numPrecision');
                        break;
                    case "reality":
                        $row[] = self::formattingFigure($pickNum - $refundNum, 'numPrecision');
                        break;
                    case "theorynum":
                        $row[] = $theorynum;
                        break;
                    case "decNum":
                        $row[] = self::lostZero($decNum, 'numPrecision');
                        break;
                    case "stock":
                        $row[] = self::formattingFigure($pickNum - $decNum - $refundNum - $theorynum, 'numPrecision');
                        break;
                    default:
                        $row[] = isset($array[$key]) && $array[$key] ? $array[$key] : '';
                }
            }
            $body[] = $row;
        }
        $expectedNum = 0;//预计用量
        $pickNum     = 0;//领料
        $refundNum = 0;//退料
        $theorynum = 0;//理论用料数量
        $resAll    = find(array(
            "table" => self::tableMx . " as a join " . self::table . " as b on a.orderId = b.id join material as c on c.id = a.materialId",
            'field' => "a.*,b.orderNo,b.recordDate,b.supplierId,c.code,c.name as materialName,c.model",
            "where" => $where
        ));
        foreach ($resAll['array'] as $rArray) {
            $expectedNum += $rArray['num'];
            //发料
            $pickOrderAll = find(array(
                'table' => 'outsource_pick_order as a join outsource_pick_order_detail as b on a.id = b.orderId',
                "where" => "a.state = '已确认' and a.type=0 and b.sourceId = '$rArray[id]' and b.materialId = '$rArray[materialId]'"
            ));
            foreach ($pickOrderAll['array'] as $val) {
                $inventoryAll = find(array('table' => 'inventory_detail', 'where' => "sourceId = '$val[id]'"));
                foreach ($inventoryAll['array'] as $inventory) {
                    $pickNum += inventoryBalanceClass::toMainUnit($val['materialId'], $inventory['num'], $inventory['unit']);
                }
            }
            $pickNum = self::formattingFigure($pickNum, 'numPrecision');

            $refundOrderAll = find(array(
                'table' => 'outsource_pick_order as a join outsource_pick_order_detail as b on a.id = b.orderId',
                "where" => "a.state = '已确认' and a.type=1 and b.sourceId = '$rArray[id]' and b.materialId = '$rArray[materialId]'"
            ));
            foreach ($refundOrderAll['array'] as $val) {
                $inventoryRefundAll = find(array('table' => 'inventory_detail', 'where' => "sourceId = '$val[id]'"));
                foreach ($inventoryRefundAll['array'] as $refundInventory) {
                    $refundNum += inventoryBalanceClass::toMainUnit($val['materialId'], $refundInventory['num'], $refundInventory['unit']);
                }
            }
            $refundOrderAll = find(array(
                'table' => 'outsource_pick_order as a join outsource_pick_order_detail as b on a.id = b.orderId',
                "where" => "a.state = '已确认' and a.type=1 and b.sourceId in (select id from outsource_pick_order_detail where sourceId = '$rArray[id]') and b.materialId = '$rArray[materialId]'"
            ));
            foreach ($refundOrderAll['array'] as $val) {
                $inventoryRefundAll = find(array('table' => 'inventory_detail', 'where' => "sourceId = '$val[id]'"));
                foreach ($inventoryRefundAll['array'] as $refundInventory) {
                    $refundNum += inventoryBalanceClass::toMainUnit($val['materialId'], $refundInventory['num'], $refundInventory['unit']);
                }
            }
            $refundNum = self::formattingFigure($refundNum, 'numPrecision');
        }
        $resAll = find(array(
            'table' => self::table_material . " as mat join " . self::tableMx . " as a on a.targetSkuId = mat.id join " . self::table . " as b on a.orderId = b.id  join material as c on c.id = a.materialId ",
            'field' => "mat.*,a.materialId as productId,c.unit as toUnit",
            'where' => $where
        ));
        foreach ($resAll['array'] as $rArray) {
            //已收货数量
            $receiveNum  = 0;
            $getOrderAll = find(array(
                'table' => 'outsource_in_order as a join outsource_in_order_detail as b on a.id = b.orderId',
                "where" => "a.state = '已确认' and b.sourceId = '$rArray[id]' and b.materialId = '$rArray[materialId]'"
            ));
            foreach ($getOrderAll['array'] as $val) {
                $inventoryRefundAll = find(array('table' => 'inventory_detail', 'where' => "sourceId = '$val[id]'"));
                foreach ($inventoryRefundAll['array'] as $refundInventory) {
                    $receiveNum += machiningOrderClass::material_num_change($val['materialId'], $refundInventory['unit'], $rArray['unit'], $refundInventory['num']);
                }
                $refundMaterialNum = 0;
                //退货数量
                $all = find(array(
                    'table' => 'outsource_refund_order as a join outsource_refund_order_detail as b on a.id = b.orderId',
                    'field' => "b.*", "where" => "a.state = '已确认' and b.sourceId = '$val[id]' and b.materialId = '$val[materialId]'"
                ));
                foreach ($all['array'] as $item) {
                    $inventoryRefundAll = find(array('table' => 'inventory_detail', 'where' => "sourceId = '$val[id]'"));
                    foreach ($inventoryRefundAll['array'] as $refundInventory) {
                        $refundMaterialNum += machiningOrderClass::material_num_change($item['materialId'], $refundInventory['unit'], $rArray['unit'], $refundInventory['num']);
                    }
                }
                $receiveNum = $receiveNum - $refundMaterialNum;//收货减退货的
            }
            $theorynum += machiningOrderClass::material_num_change(
                $rArray['productId'],
                $rArray['unit'],
                $rArray['toUnit'],
                $this->bomNum($rArray['bomId'], $rArray['materialId'], $receiveNum, $rArray['unit'], $rArray['productId'])
            );
        }
        //已冲减数量
        $decNum = find(array(
            'table' => 'outsource_settle_material',
            'where' => "outsourceMaterialId in (select a.targetSkuId from " . self::tableMx . " as a join " . self::table . " as b on a.orderId = b.id  join material as c on c.id = a.materialId where 1=1 {$where})",
            'sum'   => "num"
        ));
        $total  = [
            'expectedNum'   => $expectedNum,
            'pickNum'   => self::lostZero($pickNum, 'numPrecision'),
            'refundNum' => self::lostZero($refundNum, 'numPrecision'),
            'reality'   => self::formattingFigure($pickNum - $refundNum, 'numPrecision'),
            'theorynum'       => $theorynum,
            'decNum'    => self::lostZero($decNum, 'numPrecision'),
            'stock'     => self::formattingFigure($pickNum - $decNum - $refundNum - $theorynum, 'numPrecision'),
        ];
        //生成页面
        $li = $this->li(array(
            "body"        => $body,
            "install"     => $installSearch,
            "button"      => array("html" => $liButton),
            "orderBySign" => $orderBy,
            "page"        => $page['para'],
            "total"       => $total,
        ));
        //返回
        $json['html'] = "<form name='listForm'>" . $li . "</form>" . $page['button'] . clear;
        return json_encode($json, 320);
    }

    /**
     * 导出
     */
    public function exportDataOther()
    {
        $templateId     = $this->get('templateId');
//        $checkedId = $this->get('checkedId')?explode(',',$this->get('checkedId')):[];
        if (!$this->power('outsourceDiffStatistics', 'export')) {
            $_SESSION['warn'] = '你还没有导出的权限';
        } else {
            $data     = [];
            $sql      = $_SESSION[self::page]['stasql'];
            $where    = $_SESSION[self::page]['where'];
            $whereTwo = $_SESSION[self::page]['whereTwo'];
            /***********标题**********/
            $title = [];
            $tempFieldAll = importTemplateClass::templateField(self::page, 'exportFieldInit', id:$templateId);
            foreach ($tempFieldAll as $array) {
                if ($array['display']) {
                    $title[] = $array['aliasName'];
                }
            }
            $result = sqlFind($sql);
            //导出数据生成
            foreach ($result as $array) {
                $column      = [];
                $material    = query('material', "id = '$array[materialId]'");
                $expectedNum = 0;//预计用量
                $pickNum     = 0;
                //退料
                $refundNum = 0;
                $theorynum = 0;//理论用料数量
                $resAll    = find(array(
                    "table" => self::tableMx . " as a join " . self::table . " as b on a.orderId = b.id join material as c on c.id = a.materialId",
                    'field' => "a.*,b.orderNo,b.recordDate,b.supplierId,c.code,c.name as materialName,c.model",
                    "where" => $where . " and b.supplierId = '$array[supplierId]' and a.materialId = '$array[materialId]' order by b.recordDate desc,a.list asc"
                ));
                foreach ($resAll['array'] as $rArray) {
                    $expectedNum += $rArray['num'];
                    //发料
                    $pickOrderAll = find(array(
                        'table' => 'outsource_pick_order as a join outsource_pick_order_detail as b on a.id = b.orderId',
                        "where" => "a.state = '已确认' and a.type=0 and b.sourceId = '$rArray[id]' and b.materialId = '$rArray[materialId]'"
                    ));
                    foreach ($pickOrderAll['array'] as $val) {
                        $inventoryAll = find(array('table' => 'inventory_detail', 'where' => "sourceId = '$val[id]'"));
                        foreach ($inventoryAll['array'] as $inventory) {
                            $pickNum += machiningOrderClass::material_num_change($val['materialId'], $inventory['unit'], $material['unit'], $inventory['num']);
                        }
                    }
                    $pickNum = self::formattingFigure($pickNum, 'numPrecision');

                    $refundOrderAll = find(array(
                        'table' => 'outsource_pick_order as a join outsource_pick_order_detail as b on a.id = b.orderId',
                        "where" => "a.state = '已确认' and a.type=1 and b.sourceId = '$rArray[id]' and b.materialId = '$rArray[materialId]'"
                    ));
                    foreach ($refundOrderAll['array'] as $val) {
                        $inventoryRefundAll = find(array('table' => 'inventory_detail', 'where' => "sourceId = '$val[id]'"));
                        foreach ($inventoryRefundAll['array'] as $refundInventory) {
                            $refundNum += machiningOrderClass::material_num_change($val['materialId'], $refundInventory['unit'], $material['unit'], $refundInventory['num']);
                        }
                    }
                    $refundOrderAll = find(array(
                        'table' => 'outsource_pick_order as a join outsource_pick_order_detail as b on a.id = b.orderId',
                        "where" => "a.state = '已确认' and a.type=1 and b.sourceId in (select id from outsource_pick_order_detail where sourceId = '$rArray[id]') and b.materialId = '$rArray[materialId]'"
                    ));
                    foreach ($refundOrderAll['array'] as $val) {
                        $inventoryRefundAll = find(array('table' => 'inventory_detail', 'where' => "sourceId = '$val[id]'"));
                        foreach ($inventoryRefundAll['array'] as $refundInventory) {
                            $refundNum += machiningOrderClass::material_num_change($val['materialId'], $refundInventory['unit'], $material['unit'], $refundInventory['num']);
                        }
                    }
                    $refundNum = self::formattingFigure($refundNum, 'numPrecision');
                }
                $resAll = find(array(
                    'table' => self::table_material . " as a join " . self::table . " as b on a.orderId = b.id",
                    'field' => "a.*,b.id as outsourceId", 'where' => $whereTwo . " and b.supplierId = '$array[supplierId]'"
                ));
                foreach ($resAll['array'] as $rArray) {
                    //已收货数量-退货数量
                    $receiveNum  = 0;
                    $getOrderAll = find(array(
                        'table' => 'outsource_in_order as a join outsource_in_order_detail as b on a.id = b.orderId',
                        "where" => "a.state = '已确认' and b.sourceId = '$rArray[id]' and b.materialId = '$rArray[materialId]'"
                    ));
                    foreach ($getOrderAll['array'] as $val) {
                        $inventoryRefundAll = find(array('table' => 'inventory_detail', 'where' => "sourceId = '$val[id]'"));
                        foreach ($inventoryRefundAll['array'] as $refundInventory) {
                            $receiveNum += machiningOrderClass::material_num_change($val['materialId'], $refundInventory['unit'], $rArray['unit'], $refundInventory['num']);
                        }
                        $refundMaterialNum = 0;
                        //退货数量
                        $refundAll = find(array(
                            'table' => 'outsource_refund_order as a join outsource_refund_order_detail as b on a.id = b.orderId',
                            "where" => "a.state = '已确认' and b.sourceId = '$val[id]' and b.materialId = '$val[materialId]'"
                        ));
                        foreach ($refundAll['array'] as $item) {
                            $inventoryRefundAll = find(array('table' => 'inventory_detail', 'where' => "sourceId = '$val[id]'"));
                            foreach ($inventoryRefundAll['array'] as $refundInventory) {
                                $refundMaterialNum += machiningOrderClass::material_num_change($item['materialId'], $refundInventory['unit'], $rArray['unit'], $refundInventory['num']);
                            }
                        }
                        $receiveNum = $receiveNum - $refundMaterialNum;//收货减退货的
                    }
                    $theorynum += machiningOrderClass::material_num_change(
                        $rArray['materialId'],
                        $material['unit'],
                        $material['unit'],
                        $this->bomNum($rArray['bomId'], $rArray['materialId'], $receiveNum, $rArray['unit'], $array['materialId'])
                    );
                }
                //已冲减数量
                $decNum = find(array(
                    'table' => 'outsource_settle_material as a join outsource_settle as b on a.orderId = b.id',
                    'where' => "b.supplierId = '$array[supplierId]' and materialId = '$array[materialId]'",
                    'sum'   => "num"
                ));

                $supplier = query('supplier', "id = '$array[supplierId]'");
                $process  = query('process', "id = '$array[processId]'", "name");
                foreach ($tempFieldAll as $templateField) {
                    if ($templateField['display']) {
                        $column[] = match ($templateField['columnName']) {
                            '委外单位' => $supplier['name'],
                            '物料编码' => $material['code'],
                            '物料名称' => $material['name'],
                            '规格型号' => $material['model'],
                            '单位' => $material['unit'],
                            '工序' => $process['name'],
                            '预计用量' => $expectedNum,
                            '发料数量' => $pickNum,
                            '退料数量' => $refundNum,
                            '实际发料' => self::formattingFigure($pickNum - $refundNum, 'numPrecision'),
                            '已收货理论用料' => $theorynum,
                            '已冲减数量' => self::lostZero($decNum, 'numPrecision'),
                            '理论库存' => self::formattingFigure($pickNum - $decNum - $refundNum - $theorynum, 'numPrecision'),
                            default => '',
                        };
                    }
                }
                $data[] = $column;
            }
            $text = $this->control['name'] . "导出了委外用料差异汇总";
            $this->logText(["type" => 'outsourceDiffStatistics', "text" => $text]);
            $this->excelOut($title, $data, "委外用料差异汇总.xlsx");
            exit(0);
        }
        header("Location:" . getenv("HTTP_REFERER"));
    }

    /**
     * 导出模板字段初始化
     * @return array[]
     */
    public function exportFieldInit($classify = ''): array
    {
        return match ($classify) {
            '委外用料差异汇总' => [
                [
                    'columnName' => '委外单位',
                    'aliasName'  => '委外单位',
                ],
                [
                    'columnName' => '物料编码',
                    'aliasName'  => '物料编码',
                ],
                [
                    'columnName' => '物料名称',
                    'aliasName'  => '物料名称',
                ],
                [
                    'columnName' => '规格型号',
                    'aliasName'  => '规格型号',
                ],
                [
                    'columnName' => '单位',
                    'aliasName'  => '单位',
                ],
                [
                    'columnName' => '工序',
                    'aliasName'  => '工序',
                ],
                [
                    'columnName' => '预计用量',
                    'aliasName'  => '预计用量',
                ],
                [
                    'columnName' => '发料数量',
                    'aliasName'  => '发料数量',
                ],
                [
                    'columnName' => '退料数量',
                    'aliasName'  => '退料数量',
                ],
                [
                    'columnName' => '实际发料',
                    'aliasName'  => '实际发料',
                ],
                [
                    'columnName' => '已收货理论用料',
                    'aliasName'  => '已收货理论用料',
                ],
                [
                    'columnName' => '已冲减数量',
                    'aliasName'  => '已冲减数量',
                ],
                [
                    'columnName' => '理论库存',
                    'aliasName'  => '理论库存',
                ],
            ],
            default => [
                [
                    'columnName' => '单据日期',
                    'aliasName'  => '单据日期',
                ],
                [
                    'columnName' => '单据编号',
                    'aliasName'  => '单据编号',
                ],
                [
                    'columnName' => '委外单位',
                    'aliasName'  => '委外单位',
                ],
                [
                    'columnName' => '物料编码',
                    'aliasName'  => '物料编码',
                ],
                [
                    'columnName' => '物料名称',
                    'aliasName'  => '物料名称',
                ],
                [
                    'columnName' => '规格型号',
                    'aliasName'  => '规格型号',
                ],
                [
                    'columnName' => '单位',
                    'aliasName'  => '单位',
                ],
                [
                    'columnName' => '工序',
                    'aliasName'  => '工序',
                ],
                [
                    'columnName' => '预计用量',
                    'aliasName'  => '预计用量',
                ],
                [
                    'columnName' => '发料数量',
                    'aliasName'  => '发料数量',
                ],
                [
                    'columnName' => '退料数量',
                    'aliasName'  => '退料数量',
                ],
                [
                    'columnName' => '实际发料',
                    'aliasName'  => '实际发料',
                ],
                [
                    'columnName' => '已收货理论用料',
                    'aliasName'  => '已收货理论用料',
                ],
                [
                    'columnName' => '已冲减数量',
                    'aliasName'  => '已冲减数量',
                ],
                [
                    'columnName' => '理论库存',
                    'aliasName'  => '理论库存',
                ],
            ]
        };
    }
}

